package jzqb.service.impl;

import haha.life.common.CutPageBean;
import jzqb.bo.AdminUserInfoBo;
import jzqb.common.Result;
import jzqb.dao.AdminDepartmentDao;
import jzqb.dao.AdminUserInfoDao;
import jzqb.po.AdminDepartmentPo;
import jzqb.po.AdminLoginLogPo;
import jzqb.po.AdminLoginPo;
import jzqb.po.AdminUserPo;
import jzqb.service.AdminUserInfoService;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class AdminUserInfoServiceImpl implements AdminUserInfoService {

    @Autowired
    private AdminUserInfoDao adminuserinfodao;
    @Autowired
    private AdminDepartmentDao adminDepartmentDao;

    @Override
    public Result login(String id, String pwd){
        return adminuserinfodao.login(id, pwd);
    }

//    @Override
//    public AdminUserPo adminuserinfopoByID(String ID){
//        return adminuserinfodao.adminuserinfopoByID(ID);
//    }

//    @Override
//    public String generateLayoutList(String ID){
//        return adminuserinfodao.generateLayoutList(ID);
//    }

    @Override
    public CutPageBean<AdminUserPo> cutPage (AdminUserInfoBo adminUserinfoBo){
        CutPageBean<AdminUserPo> cutPageBean = new CutPageBean<>();
        List<AdminUserPo> adminUserList  = adminuserinfodao.cutList(adminUserinfoBo);

        //获取真实的部门名字
        List<AdminDepartmentPo> allDepartment = adminDepartmentDao.findAllDept();
        for (int i = 0; i < adminUserList.size(); i++) {
            AdminUserPo adminUserPo = adminUserList.get(i);
            List<String> showDeptList = new ArrayList<String>();
            for (int j = 0; j < adminUserPo.getDepartmentid().split(";").length; j++) {
                String singleDept = adminUserPo.getDepartmentid().split(";")[j];
                for (int k = 0; k < allDepartment.size(); k++) {
                    AdminDepartmentPo adminDepartmentPo = allDepartment.get(k);
                    if (singleDept.equals(adminDepartmentPo.getDepartmentCode())){
                        showDeptList.add(adminDepartmentPo.getDepartmentName());
                        break;
                    }
                }
            }
            adminUserPo.setShowDepartment(String.join(";",showDeptList));
        }

        int count=(int) adminuserinfodao.cutCount(adminUserinfoBo);
        cutPageBean.initCutPage(count, adminUserinfoBo.getPageSize(),adminUserList);
        return cutPageBean;
    }


    @Override
    public List<AdminUserPo> findUsersByDeptNo(String deptNo) {
        return adminuserinfodao.findUsersByDeptNo(deptNo);
    }

    @Override
    public List<AdminUserPo> getUsrByName(String userName) {
        return adminuserinfodao.getUsrByName(userName);
    }

    @Override
    public Result resetPwd (String id,String pwd){
        return adminuserinfodao.resetPwd(id, pwd);
    };

    @Override
    public Result mute (AdminUserInfoBo adminUserInfoBo){
        return adminuserinfodao.mute(adminUserInfoBo);
    };

    @Override
    public boolean belongToPermissionGroup(String userid,String PermissionGroupID){
        return adminuserinfodao.belongToPermissionGroup(userid,PermissionGroupID);
    }

    @Override
    public void insertLoginRecord(AdminLoginLogPo adminLoginLogPo){
        adminuserinfodao.insertLoginRecord(adminLoginLogPo);
    }

    @Override
    public Result initPwd(String id,String pwd,String newPwd,String newPwdConfirm){
        return adminuserinfodao.initPwd(id,pwd,newPwd,newPwdConfirm);
    }

    @Override
    public AdminUserInfoBo basicInfo(String userid){
        return adminuserinfodao.basicInfo(userid);
    };

    @Override
    public boolean isUserIDExist(String id){
        return adminuserinfodao.isUserIDExist(id);
    };


    @Override
    public boolean addUser(AdminUserPo userPo ,String type){
        return adminuserinfodao.addUser(userPo, type);
    }

    @Override
    public boolean deleteUser(AdminUserPo userPo){
        return adminuserinfodao.deleteUser(userPo);
    }


    @Override
    public List<String> getAllFunctionPermissionGroupIdsOfUser(String userid){
        return adminuserinfodao.getAllFunctionPermissionGroupIdsOfUser(userid);
    }


    @Override
    public Result changeMyPwd(AdminLoginPo adminloginpo){
        return adminuserinfodao.changeMyPwd(adminloginpo);
    }

    @Override
    public List<AdminUserPo> getAllUser(){
        return adminuserinfodao.getAllUser();
    }

    @Override
    public boolean writeExcelData(JSONArray jsonArray) throws IOException {
        return adminuserinfodao.writeExcelData(jsonArray);
    }

    @Override
    public AdminUserInfoBo getUserById(String userId) {
        return adminuserinfodao.getUserById(userId);
    }

    @Override
    public Boolean updateUserInfo(AdminUserInfoBo userPo) {
        return adminuserinfodao.updateUserInfo(userPo);
    }


}
